<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('User_model','', TRUE);
    }

    public function register() {
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required|min_length[5]|max_length[30]|is_unique[login_data.lusername]'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required|min_length[6]|max_length[30]|sha1'
            ),
            array(
                'field' => 'password_repeat',
                'label' => 'Password Confirmation',
                'rules' => 'trim|required|matches[password]'
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'trim|required|valid_email|is_unique[users.uemail]'
            )
        );

        $this->form_validation->set_rules($config);


        if ($this->form_validation->run() == FALSE) {
            $data = array(); // empty array to hold our data
            //if logged in return to home
            $this->load->helper('form');
            $this->load->helper('html');
            // generate fields
            $data['heading'] = 'Регистрация';
            $data['doctype'] = doctype('html5');
            $data['encoding'] = meta(array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'));
            $data['form']['open'] = form_open('/user/registration', array('class' => 'register_form'));
            $data['form']['username_input'] = form_input('username', '', array('maxlength' => '30'));
            $data['form']['password_input'] = form_password('password', '', array('maxlength' => '30'));
            $data['form']['password_repeat'] = form_password('password_repeat', '', array('maxlength' => '30'));
            $data['form']['mail_input'] = form_input('email', '', array('maxlength' => "30"));
            $data['form']['submit'] = form_submit('submit', "Потвърди");
            $data['form']['clear'] = form_reset('clear', "Изчисти");
            $data['form']['close'] = form_close();
            //generate form table
            $this->load->library('table');
            $this->table->add_row(array('Потребителско име', $data['form']['username_input']));
            $this->table->add_row(array('Парола', $data['form']['password_input']));
            $this->table->add_row(array('Повторете паролата', $data['form']['password_repeat']));
            $this->table->add_row(array('Електронна поща', $data['form']['mail_input']));
            $this->table->add_row(array($data['form']['clear'], $data['form']['submit']));
            $data['form_table'] = $this->table->generate();
            $this->load->view('register_view', $data);
        } else {
            $this->load->view('register_success_view');
        }
    }

    public function registration() {


        $this->load->library('User');
        $user = new User\User();
        $user->addUser('');
        var_dump($_POST);
    }

    public function login() {
        $this->load->library('form_validation');
        $currval_config = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required|min_length[5]|max_length[30]'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required|min_length[6]|max_length[30]'
            )
        );

        $this->form_validation->set_rules($currval_config);
        if ($this->form_validation->run() == FALSE) {
            $data = array(); // empty array to hold our data
            //if logged in return to home
            $this->load->helper('form');
            $this->load->helper('html');
            // generate fields
            $data['heading'] = 'Вход';
            $data['doctype'] = doctype('html5');
            $data['encoding'] = meta(array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'));
            $data['form']['open'] = form_open('/user/login', array('class' => 'login_form'));
            $data['form']['username_input'] = form_input('username', '', array('maxlength' => '30'));
            $data['form']['password_input'] = form_password('password', '', array('maxlength' => '30'));
            $data['form']['submit'] = form_submit('submit', "Влез");
            $data['form']['close'] = form_close();
            //generate form table
            $this->load->library('table');
            $this->table->add_row(array('Потребителско име', $data['form']['username_input']));
            $this->table->add_row(array('Парола', $data['form']['password_input']));
            $this->table->add_row(array($data['form']['submit']));
            $data['form_table'] = $this->table->generate();
            $this->load->view('login_view', $data);
        } else {
            $my_uid = $this->User_model->get_uid_by_user_pass($this->input->post('username'), $this->input->post('password'));
            if ($my_uid != 0) {
                echo 'set';
                $newdata = array(
                   'id'  => $my_uid,
               );
              //$this->session->set_userdata($newdata);  
              var_dump( $this->session->all_userdata());
            }
        }
    }

    public function test()
    {
        var_dump( $this->session->all_userdata());
    }
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */